package io.renren.modules.substation.controller;

import java.util.*;

import io.renren.modules.substation.entity.*;
import io.renren.modules.substation.service.TBaseDictInfoService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.substation.service.TInfoVariableInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 监测变量信息表
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-06-19 16:55:28
 */
@RestController
@RequestMapping("substation/tinfovariableinfo")
public class TInfoVariableInfoController  extends AbstractController {
    @Autowired
    private TInfoVariableInfoService tInfoVariableInfoService;

    @Autowired
    private TBaseDictInfoService tBaseDictInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tinfovariableinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tInfoVariableInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{variableId}")
    @RequiresPermissions("substation:tinfovariableinfo:info")
    public R info(@PathVariable("variableId") Long variableId){
		TInfoVariableInfoEntity tInfoVariableInfo = tInfoVariableInfoService.getById(variableId);

        return R.ok().put("tInfoVariableInfo", tInfoVariableInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tinfovariableinfo:save")
    public R save(@RequestBody TInfoVariableInfoEntity tInfoVariableInfo){

        tInfoVariableInfo.setVariableKind("00");
        tInfoVariableInfo.setCreateOpr(getUserId());
        tInfoVariableInfo.setLastOpr(getUserId());
        tInfoVariableInfo.setLastTime(new Date());
        tInfoVariableInfo.setCreateTime(new Date());

		tInfoVariableInfoService.save(tInfoVariableInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tinfovariableinfo:update")
    public R update(@RequestBody TInfoVariableInfoEntity tInfoVariableInfo){
		tInfoVariableInfoService.updateById(tInfoVariableInfo);

        return R.ok();
    }

    @RequestMapping("/getOtherTypeFLList")
    public R getOtherTypeFLList() {
        logger.debug("getTypeFLList start ");
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();

        SelectItemInfo temp0=new SelectItemInfo("--无需选择--","");
        retList.add(temp0);

        logger.debug("getTypeFLList end ");
        return R.ok().put("list", retList);
    }

    @RequestMapping("/getTypeFLList")
    public R getTypeFLList() {
        logger.debug("getTypeFLList start ");
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();

        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        retList.add(temp0);

        List<TBaseDictInfoEntity> dic_List=tBaseDictInfoService.queryByDictKey("TYPE_FL");
        for(int i=0;i<dic_List.size();i++)
        {
            SelectItemInfo temp=new SelectItemInfo(dic_List.get(i).getDictDesc(),dic_List.get(i).getDictValue());
            retList.add(temp);
        }
        logger.debug("getTypeFLList end ");
        return R.ok().put("list", retList);
    }

    @RequestMapping("/getTypeFLSHowList")
    public R getTypeFLSHowList() {
        logger.debug("getTypeFLList start ");
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();

        SelectItemInfo temp0=new SelectItemInfo("无","");
        retList.add(temp0);

        List<TBaseDictInfoEntity> dic_List=tBaseDictInfoService.queryByDictKey("TYPE_FL");
        for(int i=0;i<dic_List.size();i++)
        {
            SelectItemInfo temp=new SelectItemInfo(dic_List.get(i).getDictDesc(),dic_List.get(i).getDictValue());
            retList.add(temp);
        }
        logger.debug("getTypeFLList end ");
        return R.ok().put("list", retList);
    }

    @RequestMapping("/getVarTypeFLList")
    public R getVarTypeFLList(@RequestParam String devCode) {

        logger.debug("variableCodeNamelist start "+ devCode);
        Map<String, Object> result = new HashMap<>();

        return R.ok().put("result", result);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tinfovariableinfo:delete")
    public R delete(@RequestBody Long[] variableIds){
		tInfoVariableInfoService.removeByIds(Arrays.asList(variableIds));

        return R.ok();
    }

    @RequestMapping("/getVariableNameMap")
    public R getVariableNameMap(){
        Map<String, String> variableMap = tInfoVariableInfoService.getVariableNameMap();
        return R.ok().put("variableMap", variableMap);
    }

    @RequestMapping("/variableDesc")
    @RequiresPermissions("substation:tinfovariableinfo:variableDesc")
    public R variableDesc(){
        List<SelectItemInfo> list = new ArrayList<SelectItemInfo>();
        Map<String, String> map = new HashMap<>();
        List<TInfoVariableInfoEntity> vlist = tInfoVariableInfoService.getVariableList();
        for (TInfoVariableInfoEntity o : vlist) {
            String key = o.getVariableId() + o.getVariableKind();
            String value = o.getVariableName();
            if (!o.getVariableKind().equals("00")) {
                value = "环境量" + o.getVariableKind().substring(1, 2);
                if (!map.containsKey(value))
                    map.put(value, value);
                else
                    continue;
            }
            SelectItemInfo temp =new SelectItemInfo(value, key);
            list.add(temp);
        }
        return R.ok().put("list", list);
    }

    @RequestMapping("/variableDescByType")
    public R variableDescByType(){
        List<SelectItemInfo> list = new ArrayList<>();
        List<SelectItemInfo> list00 = new ArrayList<>();
        List<SelectItemInfo> list01 = new ArrayList<>();
        List<TInfoVariableInfoEntity> vlist = tInfoVariableInfoService.getVariableList();
        for (TInfoVariableInfoEntity o : vlist) {
            String key = o.getVariableId() + o.getVariableKind();
            String value = o.getVariableName();
            if (o.getVariableType().equals("00")) {
                SelectItemInfo temp = new SelectItemInfo(value, key);
                list00.add(temp);
            } else if (o.getVariableType().equals("01")) {
                SelectItemInfo temp = new SelectItemInfo(value, key);
                list01.add(temp);
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("list00", list00);
        result.put("list01", list01);
        return R.ok().put("result", result);
    }

    @RequestMapping("/variableDescList")
    public R variableDescList(){
        List<SelectItemInfo> list = new ArrayList<SelectItemInfo>();
        Map<String, String> map = new HashMap<>();
        List<TInfoVariableInfoEntity> vlist = tInfoVariableInfoService.getVariableList();
        for (TInfoVariableInfoEntity o : vlist) {
            String value = o.getVariableName();
            SelectItemInfo temp =new SelectItemInfo(value, value);
            list.add(temp);
        }
        return R.ok().put("list", list);
    }

}
